//
//  JKResourceManager.m
//  JKFoundation
//
//  Created by JK on 07/08/2017.
//  Copyright © 2017 Shanghai DataSeed Information Technology Co.,Ltd. All rights reserved.
//

#import "JKResourceManager.h"
#import "NSDictionary+JSON.h"
#import "NSArray+JSON.h"

@implementation JKResourceManager

+ (NSString*)bundleName
{
    return nil;
}

+ (NSString *)resourceBundlePath
{
    return [self directoryBundlePathNamed:[self bundleName]];
}

+ (NSBundle *)resourceBundle
{
    return [NSBundle bundleWithPath: [self resourceBundlePath]];
}

+ (NSString *)directoryBundlePathNamed: (NSString *)name
{
    static NSString* const JKBundleSDirName = @"Bundles";
    
    NSString * path = [[NSBundle mainBundle] pathForResource: name
                                                      ofType: @"bundle"
                                                 inDirectory: JKBundleSDirName];
    return path;
}

+ (UIImage*)imageForName: (NSString*)name
           inBundleNamed: (NSString*)bundleName
{
    NSString* bundlePath = [self directoryBundlePathNamed:bundleName];
    return [self imageForName:name inBundle:[NSBundle bundleWithPath:bundlePath]];
}

+ (UIImage *)bundleImageNamed:(NSString *)pureImageName
{
    return [self imageForName:pureImageName inBundle:[self resourceBundle]];
}

+ (UIImage*)imageForName: (NSString*)name
                inBundle: (NSBundle*)bundle
{
    return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
}

+ (UIStoryboard*)storyboardWithName:(NSString *)storyboardName
{
    return [UIStoryboard storyboardWithName:storyboardName bundle:[self resourceBundle]];
}

+ (UINib*)bundleNibNamed: (NSString*)name
{
    return [UINib nibWithNibName:name bundle:[self resourceBundle]];
}

+(NSURL*)fileUrlWithName:(NSString *)name extension:(NSString *)extension
{
    NSBundle* bundle = [self resourceBundle];
    if (!bundle) {
        return nil;
    }
    
    NSURL* urlOfFile = [bundle URLForResource:name withExtension:extension subdirectory:nil];
    if (!urlOfFile) {
        return nil;
    }
    return urlOfFile;
}

+ (NSDictionary*)dictionaryWithPlistFileOfName: (NSString*)fileName
{
    NSURL* urlOfFile = [self fileUrlWithName:fileName extension:@"plist"];
    if (!urlOfFile) {
        return nil;
    }
    return [NSDictionary dictionaryWithContentsOfURL:urlOfFile];
}

+ (NSArray*)arrayWithPlistFileOfName: (NSString*)fileName
{
    NSURL* urlOfFile = [self fileUrlWithName:fileName extension:@"plist"];
    if (!urlOfFile) {
        return nil;
    }
    return [NSArray arrayWithContentsOfURL:urlOfFile];
}

+ (NSDictionary*)dictionaryWithJSONFileOfName: (NSString*)fileName
{
    NSString* JSONString = [self stringWithJSONFileOfName:fileName];
    return [NSDictionary dictionaryWithJSONString:JSONString];
}

+ (NSArray*)arrayWithJSONFileOfName: (NSString*)fileName
{
    NSString* JSONString = [self stringWithJSONFileOfName:fileName];
    return [NSArray arrayWithJSONString:JSONString];
}

+ (NSString*)stringWithJSONFileOfName: (NSString*)fileName
{
    NSURL* urlOfFile = [self fileUrlWithName:fileName extension:@"json"];
    if (!urlOfFile) {
        return nil;
    }
    
    NSString* contentOfFile = [NSString stringWithContentsOfURL:urlOfFile encoding:NSUTF8StringEncoding error:nil];
    return contentOfFile;
}

+ (id)instantiateViewFromNibWithClass: (Class)viewClass
{
    NSString* name = NSStringFromClass(viewClass);
    return [self instantiateViewFromNibWithName:name];
}

+ (id)instantiateViewFromNibWithName: (NSString*)viewName
{
    UINib* nib = [self bundleNibNamed:viewName];
    return [nib instantiateWithOwner:nil options:nil][0];
}

@end
